<?php
namespace app\jiangnan\controller;
use think\Db;
use think\Controller;
use think\Log;
use app\service\apixcxService;
use app\service\imageService;
use app\service\wotuService;
use app\service\deviceService;
class Owneruface extends Controller{
    public function deluser()
    {
        $id=request()->post("id");
        $user_id=request()->post("user_id");
        $userinfo=Db::table("personnel")->where("id",$id)->find();
        if(empty($userinfo)){
            return json(["errCode"=>"1",'errMsg' => '该用户不存在']);
        }
        if($userinfo["parent_user_id"]!=$user_id){
            return json(["errCode"=>"1",'errMsg' => '用户删除失败']);
        }
        if($userinfo["personGuid"]!="0"){
            $result=(new wotuService())->del($userinfo["personGuid"]);
            //Log::write($result,"删除图片");
            if($result["code"]=="GS_SUS201"){
                $userinfo["del_userid"]=$user_id;
                $res=Db::table("personnel_del")->insert($userinfo);
                if($res){
                    Db::table("personnel")->where("id",$id)->delete();
                    return json(["errCode"=>"0",'msg' => '用户删除成功']);
                }else{
                    return json(["errCode"=>"1",'errMsg' => '用户删除失败1']);
                }
            }else{
                 return json(["errCode"=>"1",'errMsg' => '用户删除失败2']);
            }
           
        }else{
            $userinfo["del_userid"]=$user_id;
            $res=Db::table("personnel_del")->insert($userinfo);
            if($res){
                Db::table("personnel")->where("id",$id)->delete();
                return json(["errCode"=>"0",'msg' => '用户删除成功']);
            }else{
                return json(["errCode"=>"1",'errMsg' => '用户删除失败3']);
            }
        }
    }
	//业主直接操作家人访客上传图片
    public function piclist()
    {
        $id=request()->post("id");
        $parent_user_id=request()->post("user_id");
        $userinfo=Db::table("personnel")->where("id",$id)->find();
        if(empty($userinfo))
        {
            return json(["errCode"=>"1",'errMsg' => '该用户不存在']);
        }
        if($userinfo["personGuid"]=="0")
        {
            
            $type="2";         
            $re=(new wotuService())->reg($userinfo["name"],$userinfo["phone"],$type,"","");
            $result=json_decode($re,true);
            $guid=$result["data"]["guid"];
            Db::table("personnel")->where("id",$id)->update(["personGuid"=>$guid]);
            $userinfo["personGuid"]=$guid;
        }
        
        $ewm=dir(getcwd())->path."\static\ewm\\".$id.".png";
        if(file_exists($ewm)){
            $userinfo["ewm"]="http://www.aliyunzg.com/static/ewm/".$id.".png";
        }else{
            $resewm=(new imageService())->createimg($id,$parent_user_id);
            if($resewm)
            {
                $userinfo["ewm"]="http://www.aliyunzg.com/static/ewm/".$id.".png";
            }else
            {
                return json(["errCode"=>"1",'errMsg' => "二维码生成失败"]);
            }
        }
        $data["userinfo"]=$userinfo;
        $res=(new wotuService())->tupian($userinfo["personGuid"]);
        $data["pic"]=$res["data"];
        $num=count($res["data"]);
        $data["num"]=$num;
        $data["uploadstatus"]=true;
        if($num=="3"){
            $data["uploadstatus"]=false;
        }
        return json(["errCode"=>"0",'datainfo' =>$data]);
    }

     //上传图片处理
    public function uploadAction()
    {
        $id=request()->post("id");
        $zim_id=request()->post("zim_id");
        $user_id=request()->post("user_id");
        $data["biz_id"]=$user_id;
        $data["zim_id"]=$zim_id;
        $api=new apixcxService();
       
        $res=$api->faceimgquery(json_encode($data));
        if($res->code=="10000"){
            //查询成功
            $rest=$res->extern_info;
            $re=json_decode($rest,true);
            $img=$re["imgStr"];
            $image=$this->urlsafe_b64decode($img);
            $userinfo=Db::table("personnel")->where("id",$id)->find();
            $guid=$userinfo["personGuid"];
            $result=(new wotuService())->uploadimg($image,$guid);
            if($result["code"]=="GS_SUS600"){
                //查询该小区下的人脸设备并授权
                $locklist=(new deviceService())->userdeviceList($user_id);
                $deviceKeys="";
                if(empty($locklist)){
                    return json(["errCode"=>"0",'msg' =>"图片上传成功"]); 
                }
                foreach ($locklist as $key => $value) {
                    $deviceKeys=$deviceKeys.$value["devicekey"].",";
                }
                $deviceKeys=rtrim($deviceKeys,",");
                (new wotuService())->accredit($guid, $deviceKeys);
                
                 return json(["errCode"=>"0",'msg' =>"图片上传成功"]);
            }else{
                return json(["errCode"=>"1",'errmsg' =>"图片上传失败"]);
            }
        }
    }
    public function delimage(){
        $guid=request()->post("guid");
        $id=request()->post("id");
        $userinfo=Db::table("personnel")->where("id",$id)->find();
        if(empty($userinfo))
        {
            return json(["errCode"=>"1",'errMsg' => '图片删除失败']);
        }
        if(empty($userinfo["personGuid"])){
            return json(["errCode"=>"1",'errMsg' => '图片删除失败']);
        }
        $personGuid=$userinfo["personGuid"];
        $result=(new wotuService())->delimage($personGuid,$guid);
        //图片删除成功
        if($result["code"]=="GS_SUS601"){
            return json(["errCode"=>"1",'msg' => '图片删除成功']);
            
        }else{
            return json(["errCode"=>"1",'errMsg' => '图片删除失败']);
        }
    }
    private function urlsafe_b64decode($string) {
       $data = str_replace(array('-','_'),array('+','/'),$string);
       $mod4 = strlen($data) % 4;
       if ($mod4) {
           $data .= substr('====', $mod4);
       }
       return $data;
       return base64_decode($data);
    }
}